// index.js

import { createStoreBindings } from 'mobx-miniprogram-bindings'
import { store } from '../../store/store'
import Toast from '@vant/weapp/toast/toast';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    "urlPost": "/pages/servePost/servePost",
    "urlOrders": "/pages/ordersPath/ordersPath",
    "urlAuthen": "/pages/authentication/authentication",
    "urlRule": "/pages/rules/rules",
    "urlProblem": "/pages/problem/problem",
    "urlContact": "/pages/contact/contact",
    "urlCoupon": "/pages/coupon/coupon",
    "urlVip": "/pages/vip/vip",
    "urlMarket": "/pages/market/market",
    "urlStandard": "/pages/standard/standard",
    "urlSend": "/pages/send/send",
    "urlWode": "/pages/wode/wode",
    "urlApprove": "/pages/approve/approve",
    "icons": ["/icons/27.png", "/icons/6.png", "/icons/5.png", "/icons/1.png"],
  },

  checkUser() {
    if(this.data.user == null) {
      Toast("请先登录");
      return false;
    }
    return true;
  },

  toUrlOrders() {
    if (!this.checkUser()) {
      return;
    }
    wx.navigateTo({
      url: this.data.urlOrders,
    })
  },

  toUrlCoupon() {
    if (!this.checkUser()) {
      return;
    }
    wx.navigateTo({
      url: this.data.urlCoupon,
    })
  },

  toUrlVip() {
    if (!this.checkUser()) {
      return;
    }
    wx.navigateTo({
      url: this.data.urlVip,
    })
  },

  toUrlPost() {
    if (!this.checkUser()) {
      return;
    }
    wx.navigateTo({
      url: this.data.urlPost,
    })
  },

  toUrlSend() {
    if (!this.checkUser()) {
      return;
    }
    wx.navigateTo({
      url: this.data.urlSend,
    })
  },

  toUrlProblem() {
    if (!this.checkUser()) {
      return;
    }
    wx.navigateTo({
      url: this.data.urlProblem,
    })
  },

  toUrlAuthen() {
    if (!this.checkUser()) {
      return;
    }
    wx.navigateTo({
      url: this.data.urlAuthen,
    })
  },

  toUrlMarket() {
    if (!this.checkUser()) {
      return;
    }
    wx.navigateTo({
      url: this.data.urlMarket,
    })
  },

  toUrlApprove() {
    if (!this.checkUser()) {
      return;
    }
    if (this.data.user.userType != 2) {
      Toast("无权限访问");
      return;
    }
    wx.navigateTo({
      url: this.data.urlApprove,
    })
  },

  onLoad(options) {
    this.storeBindings = createStoreBindings(this, {
      store,
      fields: ['user'],
      actions: ['getWebsite']
    });

    let tmp = this.data.icons;
    for (let i = 0; i < tmp.length; i++) {
      tmp[i] = this.getWebsite() + tmp[i];
    }
    this.setData({
      icons: tmp,
    });
  },
})
